﻿using Blm.Utils.Extensions;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using VisionCloud.Core.Model.Ms;
using VisionCloud.Domain.DtoModel.Ms;
using VisionCloud.Domain.Repository.Interface.Ms;

namespace VisionCloud.Domain.Repository.Implement.Ms
{
    public class MsTaskMeasureItemTemplateRepository : BaseRepository<MsTaskMeasureItemTemplate>, IMsTaskMeasureItemTemplateRepository
    {
        public Guid GetTaskMeasureItemTemplateId(Guid taskId, string id)
        {
            return Db.MsTaskMeasureItemTemplates.AsNoTracking()
                .Where(p => p.TaskId == taskId && p.Id == id)
                .Select(r => r.MsTaskMeasureItemTemplateId).FirstOrDefault();
        }

        public async Task<List<MsTaskMeasureItemTemplate>> GetTaskMeasureItemTemplates(Guid taskId)
        {
            return await (
              from h in Db.MsTaskMeasureItemTemplates
              where h.TaskId == taskId
              select h
              ).AsNoTracking().ToListAsync();
        }

        public async Task<List<MsMeasureTemplateDto>> GetTaskMeasureTemplateDto(Guid taskId)
        {
            var result = await (
              from j in Db.MsTaskMeasureItemTemplates
              where j.TaskId == taskId
              select new MsMeasureTemplateDto()
              {
                  Title = j.Title,
                  Id = j.Id,
                  DisplayValue = $"{j.Id}:",
                  Type = j.Code == "r-kaijianjinshenjicha" ? 1 : j.Type,
                  Code = j.Code,
                  DesignValues = j.DesignValues.ToObject<List<DesignValue>>()
              }).OrderBy(x => x.Type).ThenBy(x => x.Title).ThenBy(y => y.Id).ToListAsync();

            if (result != null)
            {
                foreach (var mm in result)
                {
                    string value = string.Empty;
                    foreach (var des in mm.DesignValues)
                    {
                        value += $"{des.Title}:{(int)des.Value},";
                    }
                    if (!string.IsNullOrWhiteSpace(value))
                    {
                        mm.DisplayValue += value.Substring(0, value.Length - 1);
                    }
                }
                result.Sort((a, b) =>
                {
                    // 先按字母升序排序
                    if (a.Id.Substring(0, 1) != b.Id.Substring(0, 1))
                    {
                        return string.Compare(a.Id.Substring(0, 1), b.Id.Substring(0, 1));
                    }
                    // 再按数字升序排序
                    if (int.Parse(a.Id.Substring(1)) > int.Parse(b.Id.Substring(1)))
                    {
                        return 1;
                    }
                    if (int.Parse(a.Id.Substring(1)) < int.Parse(b.Id.Substring(1)))
                    {
                        return -1;
                    }
                    return 0;
                });
            }
            return result;
        }
    }
}
